using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;

namespace QuickDev.Common.JS
{
    /// <summary>
    /// Register Client Script
    /// </summary>
    public class JavaScript
    {
        /// <summary>
        /// Open a new window
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="aPage">aPage</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        public static void OpenWindow( string url , Page aPage , int width , int height )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( string.Concat( new object[] { "window.open('" , url , "','_blank','width=" , width , ",height=" , height , ",scrollbars=yes');" } ) );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "OpenWindow" , scriptText.ToString() );
        }

        /// <summary>
        /// Confirm and redirect
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="url">url</param>
        /// <param name="aPage">aPage</param>
        /// <returns></returns>
        public static void ConfirmRedirect( string message , string url , Page aPage )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "var alarm=window.confirm('" + message + "');" + "if(alarm){window.location='" + url + "';}" );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "MsgBoxRedirect" , scriptText.ToString() );
        }

        /// <summary>
        /// MsgBox alarm
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="aPage">aPage</param>
        /// <returns></returns>
        public static void MsgBoxAlarm( string message , Page aPage )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "MsgBoxMessage" , scriptText.ToString() );
        }


        /// <summary>
        /// MsgBox and redirect
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="url">url</param>
        /// <param name="aPage">aPage</param>
        public static void MsgBoxRedirect( string message , string url , Page aPage )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "window.location='" + url + "';" );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "MsgBoxRedirect" , scriptText.ToString() );
        }

        /// <summary>
        /// MsgBox and Open a new window
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="url">url</param>
        /// <param name="aPage">aPage</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <returns></returns>
        public static void MsgBoxOpenForm( string message , string url , Page aPage , int width , int height )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "window.open('" + url + "','_blank','width=" + width + ",height=" + height + "');" );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "MsgBoxOpenForm" , scriptText.ToString() );
        }

        /// <summary>
        /// MsgBox and CloseForm
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="aPage">aPage</param>
        public static void MsgBoxCloseForm( string message , Page aPage )
        {
            ClientScriptManager cs = aPage.ClientScript;
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "window.close();" );
            scriptText.Append( "</script>" );
            cs.RegisterClientScriptBlock( aPage.GetType() , "MsgBoxCloseForm" , scriptText.ToString() );
        }

        /// <summary>
        /// Alert and go to history
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="BackLong">history hierarchy</param>
        public static void AlertGoHistory( string message , int BackLong )
        {
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "history.go('" + BackLong + "')" );
            scriptText.Append( "</script>" );
            HttpContext.Current.Response.Write( scriptText.ToString() );
        }

        /// <summary>
        /// MsgBox and redirect
        /// </summary>
        /// <param name="message">message</param>
        /// <param name="url">url</param>
        public static void MsgBoxRedirect( string message , string url )
        {
            StringBuilder scriptText = new StringBuilder();
            scriptText.Append( "<script language=javascript>" );
            scriptText.Append( "alert('" + message + "');" );
            scriptText.Append( "window.location='" + url + "';" );
            scriptText.Append( "</script>" );
            HttpContext.Current.Response.Write( scriptText.ToString() );
        }
    }
}
